﻿namespace LS.Nc6.Core.Util
{
    public static  class CommonUtil
    {
        /// <summary>
        /// 转换为string[]
        /// </summary>
        /// <param name="valueAsString"></param>
        /// <returns></returns>
        public static string[] GetStringArrayNoNull(this string valueAsString)
        {

            if (!string.IsNullOrEmpty(valueAsString))
            {
                return valueAsString.Split(',', StringSplitOptions.RemoveEmptyEntries);
            }

            return new string[] { };
        }
        public static List<string> GetStringListNoNull(string strValues)
        {

            List<string> result = new List<string>();
            if (!string.IsNullOrEmpty(strValues))
            {
                return strValues.Split(',', StringSplitOptions.RemoveEmptyEntries).ToList<string>();
            }

            return result;
        }

        public static long[] GetStringToLongArryNoNull(this string valueAsString)
        {
          
            if (!string.IsNullOrEmpty(valueAsString))
            {
                
                var  strids= valueAsString.Split(',',StringSplitOptions.RemoveEmptyEntries);
                if (strids.Length > 0)
                {
                    long[] ids =new  long[strids.Length];
                    for (int i = 0; i < strids.Length; i++)
                    {
                        ids[i] = long.Parse(strids[i]);

                    }
                    return ids;
                }
            }
            return null ;

        }

        public static bool Contains<T>(IEnumerable<T> list, T value)
        {
            return list != null && list.Contains(value);
        }
        public static List<long> GetLongList(string collection, char separator = ',')
        {
            var list = new List<long>();
            if (!string.IsNullOrEmpty(collection))
            {
                var array = collection.Split(separator);
                foreach (var s in array)
                {
                    int.TryParse(s.Trim(), out var i);
                    list.Add(i);
                }
            }
            return list;
        }
        public static List<int> GetIntList(string collection, char separator = ',')
        {
            var list = new List<int>();
            if (!string.IsNullOrEmpty(collection))
            {
                var array = collection.Split(separator);
                foreach (var s in array)
                {
                    int.TryParse(s.Trim(), out var i);
                    list.Add(i);
                }
            }
            return list;
        }
        public static List<string> GetStringList(string collection, char split = ',')
        {
            var list = new List<string>();
            if (!string.IsNullOrEmpty(collection))
            {
                var array = collection.Split(split);
                foreach (var s in array)
                {
                    if (!string.IsNullOrEmpty(s))
                    {
                        list.Add(s);
                    }
                }
            }
            return list;
        }
    }
}
